<template>
  <a-card title="主机状态" :bordered="true" style="width: 100%" class="online-vms">
      <div class="online-vms-content">
        <div v-for="(item, index) in lastData" :key="index"
          :class="{ 'offline': isOffline(item), 'online': !isOffline(item), 'online-vms-item': true }">
          <div class="circle">{{ isOffline(item) ? '离线' : '在线' }}</div>
          <div class="host">{{ item.Host }}</div>
        </div>
      </div>
  </a-card>
</template>

<script setup>
import { defineProps, toRefs } from 'vue';

const props = defineProps({
  lastData: {
    type: Array,
    default: () => [],
    required: true
  }
});

const { lastData } = toRefs(props);

function isOffline(item) {
  const currentTime = Date.now();
  return (currentTime - new Date(item.Time).getTime()) > (2 * 60 * 1000);
}
</script>

<style lang="less">
.online-vms {
  display: flex;
  flex-direction: column;
  .ant-card-head-title {
    font-size: 1rem;
    font-weight: 600 !important;
  }
  .online-vms-content {
    display: flex;
    flex-direction: row;

    .online-vms-item {
      margin: 5px 30px;
    }
  }
  
}

.circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-weight: bold;
}

.offline .circle {
  background-color: #ccc;
}

.online .circle {
  background-color: #8bc34a;
}
</style>